import {useState} from 'react'
// 自定义hook
// 在hooks编程式下复用逻辑的一种方式
type IuseCounter<T> = (ini:number | undefined)=>[number,T]
type Iobj = {
    increase:(v?:number) => void
    decrease:(v?:number) => void
}

const useCounter:IuseCounter<Iobj> = (ini= 0) => {
    // 声明的状态
    let [count,setCount] = useState<number | undefined>(ini)

    // const increase = () => {
    //     setCount(count! + 1)
    // }
    // const decrease = () => {
    //     setCount(count! - 1)
    // }
    // return [count!,{increase,decrease}]

    let method:Iobj = {
        increase: (v=1) => {
            setCount(count! + v)
        },
        decrease: (v=1) => {
            setCount(count! - v)
        }
    }
    return [count!,method]

};
export default useCounter;
